package com.axis2.common.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;

/**
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author yemulin
 * @create 2012-8-24 上午11:21:08
 */
public class FileUtil {

	private static Logger log = LoggerFactory.getLogger(FileUtil.class);

	/**
	 * 读取文件内容
	 * 
	 * @param fileName
	 *            文件名称
	 * @return
	 */
	public byte[] readFile(String fileName) {
		try {
			FileInputStream fileInStream = new FileInputStream(fileName);
			ByteArrayOutputStream fileByteStream = new ByteArrayOutputStream();

			int i = 0;
			while ((i = fileInStream.read()) != -1) {
				fileByteStream.write(i);
			}
			fileInStream.close();
			return fileByteStream.toByteArray();
		} catch (Exception e) {
			log.error("Excetpion {}", e);
		}

		return null;
	}

	

	/**
	 * 写文件内容
	 * 
	 * @param fileName
	 *            文件名称
	 * @param fileStr
	 *            文件内容
	 */
	public void writeFile(String fileName, String fileStr) {
		FileOutputStream out = null;
		int count = 1;
		try {
			out = new FileOutputStream(new File(fileName));
			for (int i = 0; i < count; i++) {
				out.write(fileStr.getBytes());
			}
			out.close();
		} catch (Exception ex) {
			log.error("Excetpion {}", ex);
		} finally {
			try {
				if (out != null)
					out.close();
			} catch (IOException ex) {
				log.error("Excetpion {}", ex);
			}
		}
	}

	public boolean checkFile(String fileName){
		boolean fal=false;
		File f = new File(fileName);
		if(f.exists()){
			fal=true;
		}
		else{
			fal=false;
		}
		return fal;
	}

}
